using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using IMAPIWrapper;

namespace Ember
{
    public partial class ISOBurn : Form
    {
        public ISOBurn()
        {
            InitializeComponent();
        }

        private void ISOFileBrowseBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "ISO Files| *.ISO";
            ofd.ShowDialog();
            ISOFileBox.Text = ofd.FileName;
        }

        private void CDBurnWorker_DoWork(object sender, DoWorkEventArgs e)
        {

        }

        private void CDBurnWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            ReturnFromBurning();
        }

        private void CDBurnWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

        private void SetToBurning()
        {
            BurnBtn.Enabled = false;
            ISOFileBox.Enabled = false;
            ISOFileBrowseBtn.Enabled = false;
            IsoFileLbl.Enabled = false;
            sourceLbl.Enabled = false;
            driveSelect.Enabled = false;
            progressBar1.Visible = true;
        }
        private void ReturnFromBurning()
        {
            BurnBtn.Enabled = true;
            ISOFileBox.Enabled = true;
            ISOFileBrowseBtn.Enabled = true;
            IsoFileLbl.Enabled = true;
            sourceLbl.Enabled = true;
            driveSelect.Enabled = true;

        }

        private void BurnBtn_Click(object sender, EventArgs e)
        {
            SetToBurning();
            CDBurnWorker.RunWorkerAsync();
        }

        private void CancelBtn_Click(object sender, EventArgs e)
        {
            CDBurnWorker.CancelAsync();
        }
        private List<DiscRecorder> recorders;
        private void ISOBurn_Load(object sender, EventArgs e)
        {
            DiscMaster master = new DiscMaster();
            recorders = new List<DiscRecorder>();
            foreach (string id in master)
            {
                DiscRecorder recorder = new DiscRecorder();
                try
                {
                    recorder.InitializeDiscRecorder(id);
                }
                catch (InvalidOperationException)
                {
                    continue;
                }
                recorders.Add(recorder);
            }
            foreach (DiscRecorder dr in recorders)
                driveSelect.Items.Add(String.Format("{1} {0}", dr.ProductId.Trim(), dr.VolumePathNames.GetValue(0).ToString().Trim()));
            if (driveSelect.Items.Count == 0)
            {
                MessageBox.Show("No CD burners detected");
                SetToBurning();
                return;
            }
            driveSelect.SelectedIndex = 0;
        }
    }
}